package com.pega.eprescription.activity;

import java.io.IOException;
import java.sql.SQLException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.MotionEvent;
import android.view.Window;
import android.widget.ListView;

import com.pega.eprescription.R;
import com.pega.eprescription.config.Defines;
import com.pega.eprescription.database.manager.DatabaseManager;
import com.pega.eprescription.database.model.Pill;
import com.pega.eprescription.database.model.Prescription;
import com.pega.eprescription.utils.Utils;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class AlarmDialog extends Activity {

	private ListView mLvListSurvey;
	private AlarmDialogListAdapter mAdapListSurvey;
	private Vibrator v;
	private MediaPlayer mMediaPlayer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		final int hourOfDay = getIntent().getIntExtra(Defines.TIMEOFDAY,
				Defines.TYPE_MORNING);
		setContentView(R.layout.alarm_dialog);
		mLvListSurvey = (ListView) findViewById(R.id.prescription_list);		
		
		
		final Prescription prescr = DatabaseManager.getInstance(
				getApplicationContext()).getCurrentPrescription();
		try {
			mAdapListSurvey = new AlarmDialogListAdapter(this, DatabaseManager
					.getInstance(getApplicationContext())
					.getAllPillOfPrescriptionInTimeOfDay(prescr, hourOfDay));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mLvListSurvey.setAdapter(mAdapListSurvey);
		v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		// Start immediately
		// Vibrate for 200 milliseconds
		// Sleep for 500 milliseconds
		long[] pattern = { 0, 500, 500 };

		// The "0" means to repeat the pattern starting at the beginning
		// CUIDADO: If you start at the wrong index (e.g., 1) then your pattern
		// will be off --
		// You will vibrate for your pause times and pause for your vibrate
		// times !
		v.vibrate(pattern, 0);
		playSound(this, getAlarmUri());

		Button btnDone = (Button) findViewById(R.id.btnDone);
		btnDone.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View vi) {
				DatabaseManager.getInstance(getApplicationContext())
						.checkTakeMedicineFail(prescr, hourOfDay);
				mMediaPlayer.stop();
				v.cancel();
				finish();
				startActivity(new Intent(AlarmDialog.this,
						DailyRemindActivity.class));
			}
		});
		Button btnFoget = (Button) findViewById(R.id.btnForget);
		btnFoget.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View vi) {
				// TODO Auto-generated method stub
				DatabaseManager.getInstance(getApplicationContext())
						.checkTakeMedicineFail(prescr, hourOfDay);
				mMediaPlayer.stop();
				v.cancel();
				finish();
			}
		});

	}

	private void playSound(Context context, Uri alert) {
		mMediaPlayer = new MediaPlayer();
		try {
			mMediaPlayer.setDataSource(context, alert);
			final AudioManager audioManager = (AudioManager) context
					.getSystemService(Context.AUDIO_SERVICE);
			if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
				mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
				mMediaPlayer
						.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

							@Override
							public void onCompletion(MediaPlayer mp) {
								v.cancel();
							}
						});
				mMediaPlayer.prepare();

				mMediaPlayer.start();
			}
		} catch (IOException e) {
			System.out.println("OOPS");
		}
	}

	private Uri getAlarmUri() {
		Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
		if (alert == null) {
			alert = RingtoneManager
					.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
			if (alert == null) {
				alert = RingtoneManager
						.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
			}
		}
		return alert;
	}
}
